Skip to content

[Python] Make Beam the only retry layer in AnthropicModelHandler - #41359

Merged
damccorm merged 1 commit into
apache:masterfrom
tannnmayy:anthropic-retries
Nov 23, 2026
Merged

damccorm merged 1 commit into
apache:masterfrom
tannnmayy:anthropic-retries

Conversation

@tannnmayy

Copy link
Copy Markdown
Contributor

AnthropicModelHandler creates the client with its default max_retries=2, so the client retries inside each of RemoteModelHandler's attempts. A request that keeps failing with 429 or 500 is sent 18 times instead of 6, and Beam's throttler only sees 6 of those failures. OpenAIModelHandler already sets max_retries=0 for this reason.

This PR:

  • creates the client with max_retries=0;
  • retries APIConnectionError in the retry filter. Until now only the client retried connection errors, so without this they would no longer be retried.

Testing:

  • Updated and new tests in anthropic_inference_test.py.
  • HTTP requests counted through the real client with a mock transport (anthropic 2.4.0). With 429 or 500 on every request: 18 requests on master, 6 with this change. With a connection error on the first 3 requests: the batch fails on master and succeeds on the 4th request with this change.

This continues #40914 and #40929: I've been going through how the remote LLM handlers behave when requests fail. If there's something else in this area you'd rather I work on, I'm happy to do that instead.

R: @jrmccluskey

The Anthropic client retries failed requests twice by default, inside each
of RemoteModelHandler's attempts, so a request that keeps failing was sent
18 times and the client-side throttler only saw a third of the failures.
Create the client with max_retries=0, as OpenAIModelHandler does, and retry
connection errors in the retry filter, which until now only the client
retried.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR disables the Anthropic SDK's built-in retries (max_retries=0) in AnthropicModelHandler.create_client() and adds APIConnectionError to _retry_on_appropriate_error, ensuring Apache Beam's RemoteModelHandler and ReactiveThrottler act as the single retry and throttling layer. The implementation is clean, consistent with OpenAIModelHandler, and well-covered by unit tests.

🔍 General Feedback

  • Consistent retry handling across LLM handlers: Setting max_retries=0 on the Anthropic client and catching APIConnectionError (which also covers APITimeoutError) in _retry_on_appropriate_error matches the pattern established in OpenAIModelHandler and prevents multiplicative retries (18 attempts instead of 6) from hiding transient failures from Beam's client-side throttler.
  • Follow-up consideration (inference_args mutation on retry): Now that even a single transient failure (APIConnectionError, 429, or 5xx) is retried at the RemoteModelHandler.run_inference level rather than inside client.messages.create(), AnthropicModelHandler.request() is re-invoked with the same inference_args dict reference on every retry. Because message_from_string and message_from_conversation call inference_args.pop('max_tokens', 1024) in place without AnthropicModelHandler.request() copying inference_args first, a caller-supplied max_tokens will be lost on retry attempts (falling back to 1024). Copying inference_args in request() (as OpenAIModelHandler does) would be a great follow-up fix.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@Mola-maker Mola-maker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran anthropic_inference_test.py locally: 22 passed, 2 failed. The 2 failures (test_pipeline_e2e, test_pipeline_with_system_prompt) fail identically on the base commit, they are environment failures in my sandbox (the Anthropic SDK's httpx client chokes on a proxy env var), not a regression from this PR. The PR's own new test test_retry_on_connection_error and the updated max_retries=0 assertions pass. The change itself looks right: with max_retries=0 the client no longer retries inside Beam's attempts, and APIConnectionError is now retried by Beam's filter.

@github-actions

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @damccorm

@damccorm damccorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - LGTM, but I'll wait for checks to complete before merging

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.21%. Comparing base (de0e417) to head (67be42f).
⚠️ Report is 124 commits behind head on master.

Files with missing lines Patch % Lines
...on/apache_beam/ml/inference/anthropic_inference.py 0.00% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41359      +/-   ##
============================================
- Coverage     59.32%   59.21%   -0.11%     
  Complexity     4995     4995              
============================================
  Files          1463     1463              
  Lines        186618   186621       +3     
  Branches       3729     3729              
============================================
- Hits         110704   110514     -190     
- Misses        72543    72736     +193     
  Partials       3371     3371              
Flag Coverage Δ
python 80.51% <0.00%> (-0.23%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@damccorm
damccorm merged commit 3659062 into apache:master Nov 23, 2026
85 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants